using PKGames.Analytics;
using System.Diagnostics;
using VoxelGame.Character;

namespace PKGames.Voxel.Analytics
{
	public class ToolInteractionEvent : AnalyticsEvent
	{
		public readonly CharacterToolsController.ToolData.Tools ToolType;

		private const string EventName = "tool_interaction";

		private const string ToolTypeKey = "toolType";

		private const string TimeUsedKey = "timeUsed";

		private const string BlocksAddedKey = "blocksAdded";

		private const string BlocksRemovedKey = "blocksRemoved";

		private const int EventRevision = 1;

		private int _blocksAdded;

		private int _blocksRemoved;

		private readonly Stopwatch _toolUseStopwatch = new Stopwatch();

		protected override int Flags => 3;

		protected override string Name => "tool_interaction";

		protected override int Revision => 1;

		public ToolInteractionEvent(CharacterToolsController.ToolData.Tools toolType)
		{
			ToolType = toolType;
		}

		public void ToolUseStarted()
		{
			_toolUseStopwatch.Start();
		}

		public void ToolUseStopped()
		{
			_toolUseStopwatch.Stop();
		}

		public void BlockAdded()
		{
			_blocksAdded++;
		}

		public void BlockRemoved(int count)
		{
			_blocksRemoved += count;
		}

		public override void Finish()
		{
			base.Parameters["toolType"] = new UnityEncodedPrimitiveData(ToolType.ToString());
			_toolUseStopwatch.Stop();
			base.Parameters["timeUsed"] = new UnityEncodedPrimitiveData((float)_toolUseStopwatch.ElapsedMilliseconds / 1000f);
			base.Parameters["blocksAdded"] = new UnityEncodedPrimitiveData(_blocksAdded);
			base.Parameters["blocksRemoved"] = new UnityEncodedPrimitiveData(_blocksRemoved);
		}

		public void LogIfValid()
		{
			if (_toolUseStopwatch.ElapsedMilliseconds > 1000)
			{
				Log();
			}
		}
	}
}
